package nsu.edu.zsq.controller.sys;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.SchRoom;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.exception.ParamException;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.SchRoomService;
import nsu.edu.zsq.util.UploadFileUtil;

/** 
* Description: 学校教室的controller类<br>
*/
@RequestMapping("/sys/school/room")
@Controller
public class SchRoomController {
    
    @Autowired
    private SchRoomService schRoomService;
    
    /**
     * 获取教学楼列表
     * @return JsonData
     */
    @RequestMapping("/listBuildName")
    @ResponseBody
    public JsonData listBuildName() {
        List<String> listBuildName = schRoomService.listBuildName();
        return JsonData.success(listBuildName);
    }
    
    /**
     * 根据教学楼名称获取教室的分页数据
     * @param buildName 教学楼名称
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listSchRoom")
    @ResponseBody
    public JsonData listSchRoomWithPage(@RequestParam("buildName") String buildName, PageQuery pageQuery) {
        PageInfo<SchRoom> listSchRoom = schRoomService.listSchRoomWithPageByBuildName(buildName, pageQuery);
        return JsonData.success(listSchRoom);
    }
    
     /**
     * 保存一条教室信息
     * @param schRoom 教室信息
     * @return JsonData
     */
    @RequestMapping("/saveSchRoom")
    @ResponseBody
    public JsonData saveSchRoom(SchRoom schRoom) {
        schRoomService.saveSchRoom(schRoom);
        return JsonData.success();
    }
    
    /**
     * 更新教室信息
     * @param schRoom 教室信息
     * @return JsonData
     */
    @RequestMapping("/updateSchRoom")
    @ResponseBody
    public JsonData updateSchRoom(SchRoom schRoom) {
        schRoomService.updateSchRoom(schRoom);
        return JsonData.success();
    }
    
    /** 
     * 删除单个或者批量教室
     * @param roomId 教室id
     * @return JsonData
     */
    @RequestMapping("/delSchRoom")
    @ResponseBody
    public JsonData deleteSchRoom(@RequestParam("roomId[]")int[] roomId) {
        if (roomId.length > 1) {
            // 批量删除
            schRoomService.deleteBatchSchRoom(roomId);
        } else if (roomId.length == 1) {
            // 单个删除
            schRoomService.deleteSchRoom(roomId[0]);
        } else {
            throw new ParamException("roomId无参数");
        }
        return JsonData.success();
    }
    
    
    
    /**
     * 从excel文件导入教室信息
     * @param excelFile excel文件
     * @return JsonData
     */
    @RequestMapping("/importSchRoom")
    @ResponseBody
    public JsonData importSchRoom(@RequestParam("excelFile")MultipartFile excelFile) {
        // 1.解析excel文件，保存到数据库
        schRoomService.importSchRoomFromExcel(excelFile);
        // 2.启动多线程保存文件（先保存文件会破坏MultipartFile数据）
        new Thread() {
            public void run() {
                UploadFileUtil.uploadFile(excelFile);
            }
        }.start();

        return JsonData.success();
    }
    
    /**
     * 导出教室信息到excel
     * @param response
     */
    @RequestMapping("/exportSchRoom")
    public void exportSchRoom(HttpServletResponse response) {
        schRoomService.exportSchRoomToExcel(response);
    }

}
